package com.xlf.service.base.service.impl;

import cn.hutool.core.lang.Assert;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xlf.common.core.exception.BizException;
import com.xlf.common.core.util.StringUtils;
import com.xlf.pojo.base.Language;
import com.xlf.pojo.base.Problem;
import com.xlf.pojo.base.ProblemLanguage;
import com.xlf.pojo.base.ProblemType;
import com.xlf.service.base.mapper.ProblemLanguageMapper;
import com.xlf.service.base.service.LanguageService;
import com.xlf.service.base.mapper.LanguageMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
* @author 小新
* @description 针对表【language】的数据库操作Service实现
* @createDate 2023-11-18 22:53:59
*/
@Service
public class LanguageServiceImpl extends ServiceImpl<LanguageMapper, Language>
    implements LanguageService{

    @Resource
    LanguageMapper languageMapper;

    @Resource
    ProblemLanguageMapper problemLanguageMapper;


    @Override
    public List<Language> getList(Language language) {
        LambdaQueryWrapper<Language> wrapper = new LambdaQueryWrapper<>();
        wrapper.likeRight(StringUtils.isNotEmpty(language.getName()),Language::getName,language.getName());
        List<Language> list = list(wrapper);
        return list;
    }

    @Transactional
    @Override
    public void delete(List<String> ids) {
        Assert.notNull(ids,() -> new BizException("列表不能为空"));
        ids.forEach(item -> {
            Long count = problemLanguageMapper.selectCount(new LambdaQueryWrapper<ProblemLanguage>()
                    .eq(ProblemLanguage::getLid, item));
            if(count > 0){
                throw new BizException("该语言下存在题目，不允许删除！");
            }
            removeById(item);
        });
    }

    @Override
    public boolean updateStatus(Language language) {
        return lambdaUpdate().set(Language::getStatus,language.getStatus()).eq(Language::getId,language.getId()).update();

    }
}




